package com.atguigu.jingtai;

/**
 * @version 1.0
 * @Author 马冰
 * @Date 2024/12/17 21:24
 * @注释
 */
public class StaticProxy implements Calc{

    private CalcImpl calcImpl;

    public StaticProxy(CalcImpl calcImpl){
        this.calcImpl = calcImpl;
    }

    @Override
    public int add(int a, int b) {
        int result = 0;
        try {
            System.out.println("方法名：add，参数："+a+","+b);
            result = calcImpl.add(a, b);
            System.out.println("方法名：add，结果："+result);
        } catch (Exception e) {
            System.out.println("方法名：add，异常："+e.getMessage());
            throw new RuntimeException(e);
        } finally {
            System.out.println("方法名：add，方法执行完毕");
        }
        return result;
    }

    @Override
    public int sub(int a, int b) {
        int result = 0;
        try {
            System.out.println("方法名：sub，参数："+a+","+b);
            result = calcImpl.sub(a, b);
            System.out.println("方法名：sub，结果："+result);
        } catch (Exception e) {
            System.out.println("方法名：sub，异常："+e.getMessage());
            throw new RuntimeException(e);
        } finally {
            System.out.println("方法名：sub，方法执行完毕");
        }
        return result;
    }

    @Override
    public int mul(int a, int b) {
        int result = 0;
        try {
            System.out.println("方法名：mul，参数："+a+","+b);
            result = calcImpl.mul(a, b);
            System.out.println("方法名：mul，结果："+result);
        } catch (Exception e) {
            System.out.println("方法名：mul，异常："+e.getMessage());
            throw new RuntimeException(e);
        } finally {
            System.out.println("方法名：mul，方法执行完毕");
        }
        return result;
    }

    @Override
    public int div(int a, int b) {
        int result = 0;
        try {
            System.out.println("方法名：div，参数："+a+","+b);
            result = calcImpl.div(a, b);
            System.out.println("方法名：div，结果："+result);
        } catch (Exception e) {
            System.out.println("方法名：div，异常："+e.getMessage());
            throw new RuntimeException(e);
        } finally {
            System.out.println("方法名：div，方法执行完毕");
        }
        return result;
    }
}
